var templateParams = {
    tableTheads:["用例编号", "用例名称", "测试步骤","用例类型","创建时间","创建人", "备注", "操作"],
    columnsQuery: true,
    btnTools:[{
        type:"primary",
        size:"M",
        id:"add-object",
        iconFont:"&#xe600;",
        name:"添加测试用例"
    },{
        type:"danger",
        size:"M",
        id:"batch-op",
        iconFont:"&#xe6bf;",
        name:"批量操作"
    }],
    formControls:[{
        edit:true,
        label:"ID",
        objText:"idText",
        input:[{
            hidden:true,
            name:"id"
        }]
    },
        {
            label:"用例编号",
            input:[{
                name:"case_num"
            }]
        },
        {
            required:true,
            label:"用例标题",
            input:[{
                name:"case_title"
            }]
        },
        {
            required:true,
            label:"用例类型",
            select:[{
                name:"case_type",
                option:function(){
                    let options = [{value: '', text: '无'}];
                    $.each(DICT_CONFIG.case_type, function(i, n){
                        options.push({
                            value: n, text:n
                        });
                    });
                    return options;
                }()
            }]
        },
        {
            label:"备注",
            textarea:[{
                name:"mark"
            }]
        },
        {
            name: "classify_id"
        }

    ]};

var columnsSetting = [
    {
        "data":null,
        "render":function(data, type, full, meta){
            return checkboxHmtl(data.case_title, data.id, "selectCase");
        }},
    {"data":"id"},
    ellipsisData("case_num"),
    {
        "data": "case_title",
        "width": "18%"
    },
    {
        "data":"step_count",
        "render":function(data, type, full, meta){
            var context =
                [{
                    type:"default",
                    size:"M",
                    markClass:"show-case-step",
                    name:data || 0
                }];
            return btnTextTemplate(context);
        }
     },
    ellipsisData("case_type"),
    ellipsisData("create_time"),
    ellipsisData("creator_name"),
    longTextData("mark", "case_title", "备注信息"),
    {
        "data":null,
        "render":function(data, type, full, meta){
            var context = [{
                title:"编辑",
                markClass:"object-edit",
                iconFont:"&#xe6df;"
            },{
                title:"删除",
                markClass:"object-del",
                iconFont:"&#xe6e2;"
            }];
            return btnIconTemplate(context);
        }
    }];

var eventList = {
    //打开测试步骤页面
    '.show-case-step':function(){
        var data = table.row( $(this).parents('tr') ).data();
        layer_show(data.case_title + '-测试步骤', 'webCaseStep.html?case_id=' + data.id, '100%', '100%', 2, function(layero, index){
            // layer.full(index);
            // $(window).resize(function() {
            //     layer.full(index);
            // });
        }, null, function(){
            refreshTable();
        });
    },
    "#add-object":function(){
        publish.renderParams.editPage.modeFlag = 0;
        layer_show("添加测试用例", editHtml, editPageWidth, editPageHeight.add, 1, function() {
            $("#classify_id").val(classify_id);
        });
        publish.init();
    },
    "#batch-op":function(){
        let checkList = $(".selectCase:checked");
        if (checkList.length == 0) {
            layer.msg('请至少选择一条记录！', {time: 1500});
            return;
        }
        layer.confirm(
            '请选择你需要进行的批量操作:',
            {
                title:'批量操作',
                btn:['添加到测试集', '变更目录','删除', '取消'],
                shadeClose:true,
                area: ['408px', '160px'],
                btn4:function(index){
                    layer.close(index);
                },
                btn3:function(index){
                    layer.close(index);
                    batchOp($(".selectCase:checked"), REQUEST_URL.WEB_CASE.delCase, "删除", null, "id", {classify_id:classify_id});
                }
            },function(index){
                layer.close(index);
                layer_show('选择测试集', 'chooseTestSet.html?callback=chooseSetCallback', null, null, 2);
            },function(index){
                layer.close(index);
                chooseCaseFolder();
            });
    },
    ".object-edit":function(){
        var data = table.row( $(this).parents('tr') ).data();
        publish.renderParams.editPage.modeFlag = 1;
        publish.renderParams.editPage.objId = data.id;
        publish.renderParams.editPage.currentObj = data;
        layer_show("编辑用例信息", editHtml, editPageWidth, editPageHeight.edit, 1);
        publish.init();
    },
    ".object-del":function(){
        var data = table.row( $(this).parents('tr') ).data();
        opObj("确认要删除此测试用例吗？", REQUEST_URL.WEB_CASE.delCase, {id:data.id}, this, "删除成功!");
    }
};

var classify_id;
var mySetting = {
    eventList:eventList,
    templateCallBack:function(df){
        classify_id = GetQueryString("classify_id");
        let classify_level = GetQueryString("classify_level");
        if (GLOBAL_UTILS.stringUtils.isNotBlank(classify_level)
            && (classify_level == GLOBAL_PARAM.ELEMENT_NODE_LEVEL.ROOT.value)) {
            publish.renderParams.listPage.listUrl = REQUEST_URL.WEB_CASE.listAll;
            $('#add-object').hide();
        } else {
            publish.renderParams.listPage.listUrl = REQUEST_URL.WEB_CASE.listCase + "?classify_id=" + classify_id;
        }

        $('.breadcrumb > i:eq(0)').after(" " + GetQueryString("classify_name"));
        df.resolve();
    },
    listPage:{
        listUrl:REQUEST_URL.WEB_CASE.listCase,
        tableObj:".table-sort",
        columnsSetting:columnsSetting,
        columnsJson:[0, 9],
        dtOtherSetting:{serverSide:false}
    },
    editPage:{
        editUrl:REQUEST_URL.WEB_CASE.editCase,
        getUrl:REQUEST_URL.WEB_CASE.getCase,
        rules:{
            case_title:{
                required:true,
                minlength:2,
                maxlength:255
            }
        }
    },
    templateParams:templateParams
};

$(function(){
    publish.renderParams = $.extend(true,publish.renderParams,mySetting);
    publish.init();
});


function chooseSetCallback(obj) {
    let checkList = $(".selectCase:checked");

    layer.confirm('确定添加选中的' + checkList.length + '条记录到测试集 [ ' + obj.set_name + ' ] 吗？', {title: '提示', icon: 0}, function(index){
        let ids = [];
        $.each(checkList ,function(i, n) {
            ids.push($(n).val());
        });

        GLOBAL_UTILS.ajaxUtils.ajaxPost(REQUEST_URL.WEB_CASE_SET.addCase2Set + '&set_id=' + obj.id, ids ,function(data){
            layer.msg(data.msg, {icon:1, time:1800});
        });

        layer.close(index);
    });
}

var zTreeSetting = {
    view: {showIcon: true, dblClickExpand: false},
    data: {
        simpleData: {
            enable:true,
            idKey: "id",
            pIdKey: "parent_classify_id",
            rootPId: 1
        },
        key: {
            name:"classify_name"
        }
    },
    callback:{
        onClick:function(event, treeId, treeNode) {
            var zTree = $.fn.zTree.getZTreeObj(treeId);
            zTree.expandNode(treeNode);
        },
        onDblClick:function (event, treeId, treeNode) {//双击之后关闭窗口
            //选择完成
            layer.close(viewIndex);
            let checkList = $(".selectCase:checked");

            layer.confirm('确定将选中的' + checkList.length + '条记录移动到目录节点 [ ' + treeNode.classify_name + ' ] 中吗？', {title: '提示', icon: 0}, function(index){
                let ids = [];
                $.each(checkList ,function(i, n) {
                    ids.push($(n).val());
                });

                GLOBAL_UTILS.ajaxUtils.ajaxPost(REQUEST_URL.WEB_CASE.moveCase + '&node_id=' + treeNode.id, ids, function(data){
                    refreshTable();
                    layer.msg('移动成功！', {icon: 1, time: 1600});
                }, null, null, '正在处理中...');

                layer.close(index);
            });
        }
    }
};


var viewIndex;
function chooseCaseFolder () {
    layer_show("请双击选择要转移到的目录", '<div class="page-container"><ul id="node-tree" class="ztree"></ul></div>', 400, null, 1, function (layero, index) {
        layero.spinModal(true);

        viewIndex = index;
        GLOBAL_UTILS.ajaxUtils.ajaxGet(REQUEST_URL.WEB_CASE.listNode, function (data) {
            $.each(data.data, function(i, n){
                n['open'] = true;
            });

            $.fn.zTree.init(layero.find("#node-tree"), zTreeSetting, data.data);
            layero.spinModal(false);
        });

    }, null, null, {offset: '18px', shadeClose: false});
}